﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pokemon
{
    enum Order
    {
        Attack,
        Item,
        Pokemon,
        Flee,
        //Safari shit because Safari shit is different.
        Rock,
        Catch,
        Feed,
    }

    class Fight
    {
        public List<Trainer> Trainers;
        public List<Pokemon> Pokemon;
        public int PokemonRatio;

        public Fight(List<Trainer> Fighters)
        {
            Trainers = Fighters.ToList();
            BattleStart();
        }

        public bool GiveOrder(Order order, int[] param)
        {
            switch (order)
            {
                //param[0] (0..*) user index
                case (Order.Attack):
                    //param[1] (0..*) pokemon index
                    //param[2] (0..3) move index
                    //param[3] (0..*) target index
                    return Attack(param[0], param[1], param[2], param[3]);
                case (Order.Item): 
                    //param[1] (0..5) bag index
                    //param[2] (0..*) item index
                    //param[3] (0..*) target index - 0..5 is team.
                    break;
                case (Order.Pokemon): 
                    //param[1] (0..5) index of pokemon in team
                    break;
                case (Order.Flee):
                    return Flee(param[0]);
            }

            return false;
        }

        public bool Attack(int userindex,int pokeindex,int moveindex, int targetindex)
        {
            Trainer user = null;
            Move usemove = null;

            if (userindex < Trainers.Count)
            {
                user = Trainers[userindex];
            }

            if(Pokemon[pokeindex] != null)
            {
                usemove = Pokemon[pokeindex].Moves[moveindex];
            }

            if (usemove != null)
            {
                if (targetindex < Pokemon.Count && Pokemon[targetindex] != null)
                {
                    usemove.ActiveEffect(Pokemon[targetindex],Pokemon[pokeindex]);
                    return true;
                }
            }

            return false;
        }

        public bool Item(int userindex,int bagindex, int itemindex, int targetindex)
        {
            return false;
        }

        public bool Flee(int userindex)
        {


            return false;
        }

        private List<Pokemon> GetTrainerPokemon(Trainer t)
        {
            var r = new List<Pokemon>();

            foreach (Pokemon p in Pokemon)
            {
                if (t.Team.Contains(p))
                {
                    r.Add(p);
                }
            }

            return r;
        }

        public void BattleStart()
        {
        }

        public void Heartbeat()
        {
            foreach (Pokemon p in Pokemon)
            {
                foreach (Effect e in p.Effects)
                {
                    e.MoveEffect.PassiveEffect(p,e.User);
                }
            }
        }
    }
}
